<template>
  <el-form :model="form" class="form" label-width="110px" label-position="right">
    <el-form-item label=" 父级区域">
      <el-cascader v-model="form.regions" :disabled="popData.type === 2" :props="props" clearable />
    </el-form-item>
    <el-form-item label=" 名称">
      <el-input v-model="form.name" :disabled="popData.type === 2" placeholder="请填写 名称" />
    </el-form-item>
    <el-row>
      <el-col :span="6">
        <el-form-item>
          <el-button type="primary" @click="edit">{{ loadingText }}</el-button>
        </el-form-item>
      </el-col>
      <el-col :span="6">
        <el-form-item>
          <router-link :to="`/provinceRegions/index`" style="margin-right: 5px">
            <el-button type="primary">取消</el-button>
          </router-link>
        </el-form-item>

      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import sysProvinceRegions from '@/api/provinceRegions'

export default {
  props: ['popData'],
  data() {
    return {
      form: {},
      loadingText: '保存',
      id: 0,
      level: 1,
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          sysProvinceRegions.getByParentId({ parentId: node.value }).then(resp => {
            const nodes = resp.data.map(item => ({
              value: item.id,
              label: item.name,
              leaf: item.level > 1
            }))
            resolve(nodes)
          })
        }
      }
    }
  },
  created() {
    const id = this.popData.id
    if (id) {
      this.form.id = id
      this.loadingText = '编辑'
      this.getDetailById(id)
    } else {
      this.loadingText = '保存'
    }
  },
  methods: {
    getDetailById(id) {
      sysProvinceRegions.getById(id).then(res => {
        this.form = res.data
      })
    },
    edit() {
      this.form.parentId = this.form.regions.pop()
      sysProvinceRegions.edit(this.form).then(res => {
        this.$message.success(res.message)
        this.$router.push('/provinceRegions/index')
      })
    }
  }
}
</script>
<style>
  .form{
    margin: auto auto;
  }
</style>
